package li.nibble.twittertido;

import li.nibble.twittertido.model.DataBase;
import li.nibble.twittertido.webservice.Client;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Login extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (DataBase.registered(this)) {
			startActivity(new Intent(this, Main.class));
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		ProgressBar loading = (ProgressBar) findViewById(R.id.loading);
		LinearLayout buttons = (LinearLayout) findViewById(R.id.buttons);
		final EditText username = (EditText) findViewById(R.id.username);
		final EditText password = (EditText) findViewById(R.id.password);

		loading.setVisibility(View.GONE);
		buttons.setVisibility(View.VISIBLE);
		username.setVisibility(View.VISIBLE);
		password.setVisibility(View.VISIBLE);

		Button enter = (Button) findViewById(R.id.enter);
		enter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				enter(username.getText().toString(), password.getText()
						.toString());
			}
		});

		Button newUser = (Button) findViewById(R.id.create_account);
		newUser.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.getContext().startActivity(
						new Intent(v.getContext(), CreateAccount.class)
								.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			}
		});

	}
	
	@Override
	public void onBackPressed() {
		back();
	}

	public void back() {
		moveTaskToBack(true);
	}

	public void enter(String username, String password) {
		LinearLayout buttons = (LinearLayout) findViewById(R.id.buttons);
		ProgressBar loading = (ProgressBar) findViewById(R.id.loading);
		EditText et_username = (EditText) findViewById(R.id.username);
		EditText et_password = (EditText) findViewById(R.id.password);
		TextView error_text = (TextView) findViewById(R.id.error_text);

		buttons.setVisibility(View.GONE);
		et_username.setVisibility(View.GONE);
		et_password.setVisibility(View.GONE);
		loading.setVisibility(View.VISIBLE);
		error_text.setVisibility(View.GONE);

		new EnterTask(this, username, password, buttons, loading, et_username,
				et_password, error_text).execute();
	}

	public class EnterTask extends AsyncTask<Void, Void, Void> {

		String username;
		String password;
		Context context;
		LinearLayout buttons;
		ProgressBar loading;
		EditText et_username;
		EditText et_password;
		TextView error_text;

		public EnterTask(Context context, String username, String password,
				LinearLayout buttons, ProgressBar loading,
				EditText et_username, EditText et_password, TextView error_text) {
			this.context = context;
			this.username = username;
			this.password = password;
			this.buttons = buttons;
			this.loading = loading;
			this.et_username = et_username;
			this.et_password = et_password;
			this.error_text = error_text;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void voids) {
			if (username.length() > 0 && password.length() > 0) {

				int result = Client.login(username, password);
				if (result > 0) {
					if (!DataBase.registered(context)) {
						DataBase.localLogin(context, username, password,
								result, Client.getEmail(username, password));
					}
					startActivity(new Intent(context, Main.class)
							.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				} else {
					error_text.setText(context.getText(R.string.error_login));
					error_text.setVisibility(View.VISIBLE);
					buttons.setVisibility(View.VISIBLE);
					et_username.setVisibility(View.VISIBLE);
					et_password.setVisibility(View.VISIBLE);
					loading.setVisibility(View.GONE);
				}

			}
		}
	}

}